import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app import create_app, db
from app.models import User

def init_db():
    app = create_app()
    
    with app.app_context():
        # 删除所有现有表（如果存在）
        db.drop_all()
        print("Dropped all existing tables.")
        
        # 创建所有表
        db.create_all()
        print("Created all tables.")
        
        # 创建管理员用户
        if not User.query.filter_by(username='admin').first():
            admin = User(username='admin', email='admin@example.com', role='admin')
            admin.set_password('123456')
            db.session.add(admin)
            db.session.commit()
            print("Created admin user.")

if __name__ == '__main__':
    init_db() 